/*
 * UMLSynch plugin for Eclipse
 * http://umlsynch.googlecode.com
 *
 * Copyright 2008-2010, Evgeny Alexeyev
 * Licensed under the EPL - v1.0
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */

package com.googlecode.umlsynch.eclipse;



import java.util.HashMap;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.ISafeRunnable;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.SafeRunner;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

import com.googlecode.umlsynch.rpc.IEditorRpc;
import com.googlecode.umlsynch.rpc.IFinderRpc;
import com.googlecode.umlsynch.rpc.IIndexerRpc;
import com.googlecode.umlsynch.rpc.base.IDebug;
import com.googlecode.umlsynch.rpc.base.TDebug;

/**
 * The activator class controls the plug-in life cycle
 */
public class UMLSynchPlugin extends AbstractUIPlugin {

	// The plug-in ID
	public static final String PLUGIN_ID = "UMLSynch";
	private static final String UMLSYNCH_ID = "com.googlecode.umlsynch.eclipse.UMLSynchExtension";

	private class UMLSynchExtension {
		IEditorRpc editor;
		IFinderRpc finder;
		IIndexerRpc indexer;
	}
	
	private HashMap<String, UMLSynchExtension> Extensions = new HashMap();
	
	// Use debug stubs to remove ConsolePlugin
	// from dependency list
	private static IDebug debug = new TDebug(null);

	// The shared instance
	private static UMLSynchPlugin plugin;
	
	/**
	 * The constructor
	 */
	public UMLSynchPlugin() {
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
	 */
	public void start(BundleContext context) throws Exception {
		super.start(context);
		plugin = this;
		IConfigurationElement[] config = Platform.getExtensionRegistry()
		.getConfigurationElementsFor(UMLSYNCH_ID);
		try {
			for (IConfigurationElement e : config) {
				debug.Note("Find out plugin for UMLSynch: ");
				UMLSynchExtension next = null;
				Object lang = e.getAttribute("language");
				String language;
				if (lang instanceof String) {
					language = (String)lang;
					debug.Note("language is" + language);
					next = new UMLSynchExtension();
				} else {
					debug.Note("language undefined. Plugin is rejected.");
					continue;
				}

				Object editor = e.createExecutableExtension("IEditorRpc");
				if (editor instanceof IEditorRpc) {
					next.editor = (IEditorRpc)editor;
				}

				Object finder = e.createExecutableExtension("IFinderRpc");
				if (finder instanceof IFinderRpc) {
					next.finder = (IFinderRpc)finder;
				}

				Object indexer = e.createExecutableExtension("IIndexerRpc");
				if (indexer instanceof IIndexerRpc) {
					next.indexer = (IIndexerRpc)indexer;
				}
				
				Extensions.put(language, next);
			}
		} catch (CoreException ex) {
			System.out.println(ex.getMessage());
		}
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext context) throws Exception {
		plugin = null;
		super.stop(context);
	}

	/**
	 * Returns the shared instance of Debuger
	 *
	 * @return the shared instance of Debuger
	 */
	public static IDebug getDebug() {
		return debug;
	}

	/**
	 * Set the the debugger output 
	 */
	public static void setDebug(IDebug dbg) {
		debug = new TDebug(dbg);
	}

	/**
	 * Returns the shared instance
	 *
	 * @return the shared instance
	 */
	public static UMLSynchPlugin getDefault() {
		return plugin;
	}

	/**
	 * Returns an image descriptor for the image file at the given
	 * plug-in relative path
	 *
	 * @param path the path
	 * @return the image descriptor
	 */
	public static ImageDescriptor getImageDescriptor(String path) {
		return imageDescriptorFromPlugin(PLUGIN_ID, path);
	}

	/**
	 * Returns the IEditorRpc interface 
	 * implementation for the specified
	 * language if this language supported 
	 *
	 * @return IEditorRpc
	 */
	public IEditorRpc getEditor(String language) {
		if (Extensions.get(language) != null) {
			return Extensions.get(language).editor;
		}
		return null;
	}

	/**
	 * Returns the IFinderRpc interface 
	 * implementation for the specified
	 * language if this language supported 
	 *
	 * @return IFinderRpc
	 */
	public IFinderRpc getFinder(String language) {
		if (Extensions.get(language) != null) {
			return Extensions.get(language).finder;
		}
		return null;
	}

	/**
	 * Returns the IIndexerRpc interface 
	 * implementation for the specified
	 * language if this language supported 
	 *
	 * @return IIndexerRpc
	 */
	public IIndexerRpc getIndexer(String language) {
		if (Extensions.get(language) != null) {
			return Extensions.get(language).indexer;
		}
		return null;
	}
	
}
